package com.yhk.wuchat.common.user.dao;

import com.yhk.wuchat.common.common.domain.enums.YesOrNoEnum;
import com.yhk.wuchat.common.user.domain.entity.UserBackpack;
import com.yhk.wuchat.common.user.mapper.UserBackpackMapper;
import com.yhk.wuchat.common.user.service.IUserBackpackService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 用户背包表 服务实现类
 * </p>
 *
 * @author moresuo
 * @since 2024-04-08
 */
@Service
public class UserBackpackDao extends ServiceImpl<UserBackpackMapper, UserBackpack>  {


    public Integer getCountByValidItemId(Long uid, Long id) {
        return lambdaQuery().eq(UserBackpack::getUid, uid)
                .eq(UserBackpack::getItemId, id)
                .eq(UserBackpack::getStatus, YesOrNoEnum.NO.getStatus())
                .count();
    }

    public UserBackpack getFirstValidItem(Long uid, Long id) {
        return lambdaQuery().eq(UserBackpack::getUid,uid)
                .eq(UserBackpack::getItemId,id)
                .eq(UserBackpack::getStatus, YesOrNoEnum.NO.getStatus())
                .orderByAsc(UserBackpack::getId)
                .last("limit 1")
                .one();
    }

    public boolean userItem(UserBackpack modifyNameItem) {
        return lambdaUpdate().eq(UserBackpack::getId,modifyNameItem.getId())
                .eq(UserBackpack::getStatus,YesOrNoEnum.NO.getStatus())
                .set(UserBackpack::getStatus,YesOrNoEnum.YES.getStatus())
                .update();
    }

    /**
     * 获取用户背包中的同一类型物品
     * @param uid
     * @param ids
     * @return
     */
    public List<UserBackpack> getByItemIds(Long uid, List<Long> ids) {
        return lambdaQuery().eq(UserBackpack::getUid,uid)
                .eq(UserBackpack::getStatus,YesOrNoEnum.NO.getStatus())
                .in(UserBackpack::getItemId,ids)
                .list();
    }

    public UserBackpack getByIdemptent(String idempotent) {
        return lambdaQuery().eq(UserBackpack::getIdempotent, idempotent).one();
    }
}
